﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

using Kenly.DBFramework.Gateway;

namespace Kenly.DBFramework.Reflection
{
    /// <summary>
    /// 
    /// </summary>
    public static class IdentityUtility
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static PropertyInfo GetIdentityProperty(Type type)
        {
            if (type == null)
            {
                return null;
            }

            PropertyInfo[] propertyInfos = type.GetProperties();
            if (propertyInfos == null || propertyInfos.Length <= 0)
            {
                return null;
            }

            ColumnAttribute columnAttribute = null;
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                columnAttribute = AttributeUtility.GetColumnAttribute(propertyInfo);
                if (columnAttribute != null)
                {
                    if (columnAttribute.IsIdentity)
                    {
                        return propertyInfo;
                    }
                }
            }

            return null;
        }

        //public static bool ContainsIdentity(Type type)
        //{
        //    PropertyInfo propertyInfo = GetIdentityPropertyInfo(type);
        //    return (propertyInfo != null);
        //}

        /// <summary>
        /// Get Identity name from object with reflection .
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string GetIdentityName(Type type)
        {
            PropertyInfo propertyInfo = GetIdentityProperty(type);
            if (propertyInfo == null)
            {
                return "";
            }
            return AttributeUtility.GetColumnName(propertyInfo);

        }


        ///// <summary>
        ///// No use right now
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public static object GetIdentityValue(object entity)
        //{
        //    string identityName = "";
        //    object obj = new object();
        //    GetIdentity(entity, ref identityName, ref obj);
        //    return obj;
        //}

 
        /// <summary>
        /// Get Identity name and value from object with reflection .
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="identityName"></param>
        /// <param name="identityValue"></param>
        public static void GetIdentity(object entity, out string identityName, out object identityValue)
        {
            identityName = "";
            identityValue = null;
            if (entity == null)
            {
                return;
            }

            PropertyInfo propertyInfo = IdentityUtility.GetIdentityProperty(entity.GetType());
            if (propertyInfo == null)
            {
                return;
            }

            identityName = AttributeUtility.GetColumnName(propertyInfo);
            identityValue = propertyInfo.GetValue(entity, null);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="value"></param>
        public static void SetIdentityValue(object entity, object value)
        {
            if (entity == null)
            {
                return;
            }

            PropertyInfo propertyInfo = GetIdentityProperty(entity.GetType());
            if (propertyInfo == null)
            {
                return;
            }
            if (propertyInfo.CanWrite)
            {
                propertyInfo.SetValue(entity, value, null);
            }
        }

    }
}



